package com.powernode.controller;

import com.powernode.domain.Category;
import com.powernode.model.Result;
import com.powernode.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 商品类目控制器
 * @Author: Tong Ziyu
 * @Date: 2025/2/6 16:04
 */
@RestController
@Api(tags = "商品类目接口管理")
@Slf4j
@RequestMapping("prod/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;


    /**
     * 查询所有分类
     * @return
     */
    @ApiOperation("查询所有分类")
    @GetMapping("table")
    @PreAuthorize("hasAuthority('prod:category:page')")
    public Result<List<Category>> loadCategoryList(){
        // 全量查询需要设置缓存
        List<Category> list = categoryService.queryCategoryList();

        return Result.success(list);
    }

    /**
     * 查询一级分类
     * @return
     */
    @ApiOperation("查询一级分类")
    @GetMapping("listCategory")
    @PreAuthorize("hasAuthority('prod:category:page')")
    public Result<List<Category>> loadListCategory(){
        List<Category> list = categoryService.queryFirstCategoryList();
        return Result.success(list);
    }

    /**
     * 新增种类
     * @param category
     * @return
     */
    @ApiOperation("新增种类")
    @PostMapping("")
    @PreAuthorize("hasAuthority('prod:category:save')")
    public Result<String> saveCategory(@RequestBody Category category){
        Boolean i = categoryService.saveCategory(category);
        return Result.handle(i);
    }

    /**
     * 根据id查询分类
     * @param categoryId
     * @return
     */
    @ApiOperation("根据id查询分类")
    @GetMapping("info/{categoryId}")
    @PreAuthorize("hasAuthority('prod:category:info')")
    public Result<Category> infoCategoryId(@PathVariable(value = "categoryId") Long categoryId){
        Category category = categoryService.getById(categoryId);

        return Result.success(category);
    }

    /**
     * 根据id修改种类
     * @param category
     * @return
     */
    @ApiOperation("根据id修改种类")
    @PutMapping()
    @PreAuthorize("hasAuthority('prod:category:update')")
    public Result<String> updateCategoryById(@RequestBody Category category){
        Boolean flag = categoryService.modifyCategoryById(category);

        return Result.handle(flag);
    }

    /**
     * 根据Id删除种类
     * @param categoryId
     * @return
     */
    @ApiOperation("根据Id删除种类")
    @DeleteMapping("{categoryId}")
    @PreAuthorize("hasAuthority('prod:category:delete')")
    public Result<String> deleteCategoryById(@PathVariable Long categoryId){
        Boolean flag = categoryService.removeCategoryById(categoryId);
        return Result.handle(flag);
    }
}
